package com.coocaa.ccmvp.demo.v;

import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.coocaa.ccmvp.R;
import com.coocaa.ccmvp.demo.p.IDemoPresenter;

/**
 * Created by Lu on 2017/1/15.
 */

public class DemoView extends DialogFragment implements IDemoView {
    private Context mContext;
    private IDemoPresenter presenter;

    @Override
    public void showMe(FragmentManager fmanager) {
        show(fmanager, "");
    }

    @Override
    public void showTips(String tips) {
        Toast.makeText(mContext, tips, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void hideMe() {
        dismiss();
    }

    @Override
    public void attachPresenter(IDemoPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void create(Context context) {
        mContext = context;
    }

    private EditText editTextName, editTextPasswd;
    private Button buttonLogin;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.demo_layout_login_dialog, container);
        editTextName = (EditText) view.findViewById(R.id.layout_login_dailog_editText_name);
        editTextPasswd = (EditText) view.findViewById(R.id.layout_login_dailog_editText_passwd);
        buttonLogin = (Button) view.findViewById(R.id.layout_login_dailog_button_login);
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (presenter != null) {
                    String name = editTextName.getText().toString();
                    String passwd = editTextPasswd.getText().toString();
                    presenter.login(name, passwd);
                }
            }
        });
        return view;
    }

    @Override
    public void destroy() {

    }
}
